local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities)

local workspace_dir = vim.fn.fnamemodify(vim.fn.getcwd(), ':p:h:t')

local config = {
  cmd = {

    'java', -- or '/path/to/java11_or_newer/bin/java'
    '-Declipse.application=org.eclipse.jdt.ls.core.id1',
    '-Dosgi.bundles.defaultStartLevel=4',
    '-Declipse.product=org.eclipse.jdt.ls.core.product',
    '-noverify',
    '-Dlog.protocol=true',
    '-Dlog.level=ALL',
    '-Xms1g',
    '-Xmx2g',
    '--add-modules=ALL-SYSTEM',
    '--add-opens', 'java.base/java.util=ALL-UNNAMED',
    '--add-opens', 'java.base/java.lang=ALL-UNNAMED',

    '-jar', '/home/banyue/Libraries/java/jdtls_1.5/plugins/org.eclipse.equinox.app_1.6.0.v20210723-1130.jar',

    '-configuration', '/home/banyue/Libraries/java/jdtls_1.5/config_linux',

    '-data', vim.fn.expand('~/.cache/jdtls-workspace') .. workspace_dir,
  },
  root_dir = require('jdtls.setup').find_root({'.git', 'mvnw', 'gradlew'}),
    capabilities = capabilities
}    
require('jdtls').start_or_attach(config)

local map = vim.api.nvim_set_keymap
local opts = { noremap=true, silent=true}


 -- GOTO mappings
  map('n','gD','<cmd>lua vim.lsp.buf.declaration()<CR>',opts)
  map('n','gd','<cmd>lua vim.lsp.buf.definition()<CR>',opts)
  map('n','K','<cmd>lua vim.lsp.buf.hover()<CR>',opts)
  map('n','gr','<cmd>lua vim.lsp.buf.references()<CR>',opts)
  map('n','gs','<cmd>lua vim.lsp.buf.signature_help()<CR>',opts)
  map('n','gi','<cmd>lua vim.lsp.buf.implementation()<CR>',opts)
  map('n','<leader>gt','<cmd>lua vim.lsp.buf.type_definition()<CR>',opts)
  map('n','<leader>gw','<cmd>lua vim.lsp.buf.document_symbol()<CR>',opts)
  map('n','<leader>gW','<cmd>lua vim.lsp.buf.workspace_symbol()<CR>',opts)
  -- ACTION mappings
  map('n','<leader>ah',  '<cmd>lua vim.lsp.buf.hover()<CR>',opts)
  map('n','<leader>af', '<cmd>lua require"jdtls".code_action()<CR>',opts)
  map('n','<leader>ar',  '<cmd>lua vim.lsp.buf.rename()<CR>',opts)
  -- Few language severs support these three
  map('n','<leader>=',  '<cmd>lua vim.lsp.buf.formatting()<CR>',opts)
  map('n','<leader>ai',  '<cmd>lua vim.lsp.buf.incoming_calls()<CR>',opts)
  map('n','<leader>ao',  '<cmd>lua vim.lsp.buf.outgoing_calls()<CR>',opts)
  -- Diagnostics mapping
  map('n','<leader>ee', '<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>',opts)
  map('n','<leader>en', '<cmd>lua vim.lsp.diagnostic.goto_next()<CR>',opts)
  map('n','<leader>ep', '<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>',opts)

  map('n', [[<leader>ai]], [[<Cmd>lua require'jdtls'.organize_imports()<CR>]],opts)
  map('n', [[<leader>av]], [[<Cmd>lua require('jdtls').extract_variable()<CR>]],opts)
  map('v', [[<leader>av]], [[<Esc><Cmd>lua require('jdtls').extract_variable(true)<CR>]],opts)
  map('v', [[<leader>am]], [[<Esc><Cmd>lua require('jdtls').extract_method(true)<CR>]],opts)
  map('n', [[<leader>aR]], [[<Cmd>lua require('jdtls').code_action(false, 'refactor')<CR>]],opts)
